import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';

class FMSingleRadioPage extends StatefulWidget {

  String? selectData;

  List<String> mList;

  final Function(String)? onTap;

  FMSingleRadioPage({super.key, required this.mList,this.selectData,this.onTap});

  @override
  State<FMSingleRadioPage> createState() => _FMSingleRadioPageState();
}

class _FMSingleRadioPageState extends State<FMSingleRadioPage> {

  int curIndex = 0;

  @override
  void initState() {

    if (widget.selectData != null && widget.selectData!.isNotEmpty) {

      for (String str in widget.mList) {
        int index = widget.mList.indexOf(str);
        if (str == widget.selectData) {
          setState(() {
            curIndex = index;
          });
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black.withOpacity(0.5),
      body: Stack(
        fit: StackFit.expand,
        children: [
          GestureDetector(onTap: () {
            Navigator.pop(context);
          }),
          Center(
            child: Container(
              height: 504,
              width: 311,
              alignment: Alignment.topCenter,
              margin: const EdgeInsets.symmetric(horizontal: 32),
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/address/ic_address_dialog_bg.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    margin: const EdgeInsets.only(top: 18, bottom: 10),
                    child: const Text(
                      'Title',
                      style: TextStyle(color: Color(0xFF333333), fontSize: 20),
                    ),
                  ),
                  Container(
                    margin: const EdgeInsets.only(left: 12, right: 12, top: 0),
                    height: 317,
                    child: generateList(),
                  ),
                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      Navigator.pop(context);
                      widget.onTap!(widget.mList[curIndex]);
                    },
                    child: Container(
                      margin: const EdgeInsets.only(top: 40, left: 20, right: 20),
                      height: 47,
                      width: double.infinity,
                      alignment: Alignment.center,
                      decoration: const BoxDecoration(
                        color: Color(0xFF000000),
                        borderRadius: BorderRadius.all(Radius.circular(8)),
                        image: DecorationImage(
                          image: AssetImage('assets/address/ic_address_arrow.png'),
                        ),
                      ),
                      child: Text(
                        FMLanguageTool.getText("Confirm"),
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          fontWeight: FontWeight.w700,
                        ),
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      Navigator.pop(context);
                    },
                    child: Container(
                      margin: const EdgeInsets.only(top: 10),
                      alignment: Alignment.center,
                      child: Text(
                        FMLanguageTool.getText("Cancel"),
                        style: const TextStyle(
                          color: Color(0xFF333333),
                          fontSize: 16,
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget generateList() {
    return ListView.builder(
        itemCount: widget.mList.length,
        padding: const EdgeInsets.only(top: 20),
        itemExtent: 40,
        itemBuilder: (context, index) {
          bool isSelected = curIndex == index;
          return GestureDetector(
            onTap: () {
              setState(() {
                curIndex = index;
              });
            },
            behavior: HitTestBehavior.translucent,
            child: Container(
              height: 40,
              alignment: Alignment.center,
              margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
              decoration: BoxDecoration(
                color:
                    (isSelected ? const Color(0xFFFC7408) : Colors.transparent),
                borderRadius: const BorderRadius.all(Radius.circular(20)),
              ),
              child: Text(
                widget.mList[index],
                style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w700,
                    color: Color(0xFF333333)),
              ),
            ),
          );
        });
  }
}
